{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基本张量运算\n",
    "\n",
    "使用TensorFlow v2的基本张量操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# tf.constant()\n",
    "# value：value可以是一个数，也可以是一个list。\n",
    "# dtype：类型：float16、float32、float64、int32.\n",
    "# shape: 张量的形状\n",
    "tensor = tf.constant(1)\n",
    "print(\"张量是：\", tensor)\n",
    "print(\"张量的形状：\", tensor.shape)\n",
    "print(\"张量的类型：\", tensor.dtype)\n",
    "print(\"张量的值：\", tensor.numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建张量\n",
    "a = tf.constant(2, dtype=tf.int32)\n",
    "b = tf.constant(3)\n",
    "c = tf.constant(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 各种张量运算\n",
    "add = tf.add(a, b)\n",
    "sub = tf.subtract(a, b)\n",
    "mul = tf.multiply(a, b)\n",
    "div = tf.divide(a, b)\n",
    "\n",
    "# 获取张量计算值\n",
    "print(\"add =\", add.numpy())\n",
    "print(\"sub =\", sub.numpy())\n",
    "print(\"mul =\", mul.numpy())\n",
    "print(\"div =\", div.numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 赋值运算\n",
    "a = tf.ones([2, 3], dtype=tf.int32)  # 创建一个元素全是1的张量\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# a[0,0] = 10\n",
    "# 会报错\n",
    "# TypeError: 'tensorflow.python.framework.ops.EagerTensor' object does not support item assignment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 赋值运算\n",
    "a = tf.Variable(a)\n",
    "a[0, 0].assign(10)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 矩阵乘法\n",
    "matrix1 = tf.constant([[1, 2], [3, 4]])\n",
    "matrix2 = tf.constant([[5, 6], [7, 8]])\n",
    "\n",
    "product = tf.matmul(matrix1, matrix2)\n",
    "print(matrix1 * matrix2)  # tf.multiply(matrix1, matrix2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 显示张量\n",
    "product"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "product.numpy()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
